package com.colorfruit.admin.control.intercepter;


import com.alibaba.fastjson.JSONObject;
import com.colorfruit.admin.control.annotation.Auth4Admin;
import com.colorfruit.admin.pojo.enums.AdminRedisEnum;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.model.SysUserModel;
import com.colorfruit.framework.intercepter.BaseInterceptor;
import com.colorfruit.framework.model.pojo.BaseResult;
import com.colorfruit.framework.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Auth4AdminInterceptor extends BaseInterceptor {

    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        String method = request.getMethod();
        log.warn("请求参数头部map:{}",map);
        log.warn("response:{}",response.toString());
        log.warn("handler:{}",handler.toString());
        log.info("【管理后台权限拦截器】当前访问接口url：{}",request.getRequestURI());
        log.info("请求方式为 = {}", method);
        if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
            Auth4Admin authPassport = ((HandlerMethod) handler).getMethodAnnotation(Auth4Admin.class);
            //没有声明需要权限,或者声明不验证权限
            if(authPassport != null){
                //在这里实现自己的权限验证逻辑
                String token = request.getHeader("token");
                log.info("【管理后台权限拦截器】token：{}", AdminRedisEnum.SYS_USER_PREFIX.getKey() + token);
                String userModelString = redisCache.getCache(AdminRedisEnum.SYS_USER_PREFIX.getKey() + token, String.class);
                log.info("userModelString = {}", userModelString);
                if (StringUtils.isNoneBlank(userModelString)) {
                    //解析user对象
                    SysUserModel sysUserModel = JSONObject.parseObject(userModelString, SysUserModel.class);
                    request.getSession().setAttribute("sysUser",sysUserModel);
                    return true;
                } else {
                    response.setCharacterEncoding("UTF-8");
                    //403
                    response.setStatus(HttpStatus.FORBIDDEN.value());
                    response.getWriter().write(JSONObject.toJSONString(new BaseResult(ErrorCodeEnum.TOKEN_EXPIRED.getCode(), ErrorCodeEnum.TOKEN_EXPIRED.getMsg())));
                    return false;
                }
            }
        }
        //通过
        return true;
    }
}
